package com.zhang.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zhang.entity.User;
import com.zhang.service.UserService;
import com.zhang.service.impl.UserServiceImpl;
import com.zhang.utils.MyMailUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import redis.clients.jedis.Jedis;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@Controller
@RequestMapping("/user")
public class UserController {
    UserService us = new UserServiceImpl();

    @RequestMapping("/login")
    public String login(String email, String password, HttpSession session, Model model) throws JsonProcessingException {
        User user = us.selectByEmailAndPwd(email, password);
        if(user!=null){
            session.setAttribute("user",user);
            ObjectMapper mapper=new ObjectMapper();
            String s = mapper.writeValueAsString(user);
            Jedis jedis = new Jedis("192.168.111.107",6379);
            jedis.set(String.valueOf(user.getId()),s);
            return "redirect:/main/showAll.do";
        }else{
            return "redirect:/user/login_form.jsp?error=-1";
        }
    }

    @RequestMapping("/regist")
    public String regist(User u,String number,HttpSession session){
        String code = (String)session.getAttribute("code");
        if(code.equalsIgnoreCase(number)){
            boolean regist = us.regist(u);
            if(regist){
                session.setAttribute("ruser",u);
                return "redirect:/user/verify_form.jsp";
            }
            return "redirect:/user/register_form.jsp?error1=-1";
        }
        return "redirect:/user/register_form.jsp?error=-1";

    }

    @RequestMapping("/regist_ok")
    public String regist_ok(String emailCode,HttpSession session){
        String emailCode1 = (String)session.getAttribute("emailCode");
        if(emailCode1.equals(emailCode)){
            User ruser = (User)session.getAttribute("ruser");
            us.insert(ruser);
            ruser = us.selectByEmail(ruser.getEmail());
            session.setAttribute("user",ruser);
            return "redirect:/user/register_ok.jsp";
        }
        return "redirect:/user/verify_form.jsp?error=-1";
    }

    @RequestMapping("/handleVcode")
    public void handleVcode(HttpServletResponse response,HttpSession session) throws IOException {
        //产生验证码图片的。图片的宽是116，高是36，验证码的长度是4，干扰线的条数是20
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(116, 36, 4, 30);
        //把这个图片交给response相应给浏览器。
        //获取到response的响应流。
        ServletOutputStream outputStream = response.getOutputStream();
        //把图片放入到response的相应流中。
        lineCaptcha.write(outputStream);
        //获取到生成的验证码的值
        String code = lineCaptcha.getCode();
        session.setAttribute("code",code);
        outputStream.close();
    }

    @RequestMapping("/sendEmail")
    public String sendEmail(HttpSession session){
        User ruser = (User)session.getAttribute("ruser");
        String accept = ruser.getEmail();
        String code = MyMailUtil.sendEmail(accept);
        session.setAttribute("emailCode",code);
        return "redirect:/user/verify_form.jsp";
    }


    @RequestMapping("/clearSign")
    public String redirect(HttpSession session){
        session.invalidate();
        return "redirect:/main/showAll.do";
    }
}
